/**   
* @Title: ClaimModelMain.java 
* @Package com.infodms.dms.actions.claim.basicData 
* @Description: TODO(车型组维护) 
* @author wangjinbao   
* @date 2010-6-4 上午08:50:26 
* @version V1.0   
*/
package com.infodms.dms.actions.claim.basicData;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.infodms.dms.bean.AclUserBean;
import com.infodms.dms.common.Constant;
import com.infodms.dms.common.ErrorCodeConstant;
import com.infodms.dms.common.Utility;
import com.infodms.dms.common.getCompanyId.GetOemcompanyId;
import com.infodms.dms.dao.claim.basicData.ClaimModelDao;
import com.infodms.dms.exception.BizException;
import com.infodms.dms.po.TtAsWrModelGroupPO;
import com.infodms.dms.util.sequenceUitl.SequenceManager;
import com.infoservice.mvc.context.ActionContext;
import com.infoservice.mvc.context.RequestWrapper;
import com.infoservice.po3.bean.PageResult;

/** 
 * @ClassName: ClaimModelMain 
 * @Description: TODO(车型组维护) 
 * @author wangjinbao 
 * @date 2010-6-4 上午08:50:26 
 *  
 */
public class ClaimModelMain {
	private Logger logger = Logger.getLogger(ClaimModelMain.class);
	private final ClaimModelDao dao = ClaimModelDao.getInstance();
	private final String CLAIM_MODEL_URL = "/jsp/claim/basicData/claimModelIndex.jsp";//主页面（查询）
	private final String CLAIM_MODEL_ADD_URL = "/jsp/claim/basicData/claimModelAdd.jsp";//增加页面
	private final String CLAIM_MODEL_MODIFY_URL = "/jsp/claim/basicData/claimModelModfiy.jsp";//修改页面
	private final String CLAIM_MODEL_DETAIL_URL = "/jsp/claim/basicData/claimModelDetail.jsp";//详细页面
	
	/**
	 * 
	* @Title: claimModelInit 
	* @Description: TODO(车型组维护页面初始化) 
	* @param    
	* @return void  
	* @throws
	 */
	public void claimModelInit(){
		ActionContext act = ActionContext.getContext();
		AclUserBean logonUser = (AclUserBean)act.getSession().get(Constant.LOGON_USER);
		try {
			act.setForword(CLAIM_MODEL_URL);
		} catch (Exception e) {
			BizException e1 = new BizException(act,e,ErrorCodeConstant.QUERY_FAILURE_CODE,"车型组维护");
			logger.error(logonUser,e1);
			act.setException(e1);
		}		
		
	}
	/**
	 * 
	* @Title: claimModelQuery 
	* @Description: TODO(车型组维护查询) 
	* @param    
	* @return void  
	* @throws
	 */
	public void claimModelQuery() {
		ActionContext act = ActionContext.getContext();
		AclUserBean logonUser = (AclUserBean)act.getSession().get(Constant.LOGON_USER);
		Long oemCompanyId = GetOemcompanyId.getOemCompanyId(logonUser);
		List<Object> params = new LinkedList<Object>();
		StringBuffer sb = new StringBuffer();
		try {
			RequestWrapper request = act.getRequest();
			act.getResponse().setContentType("application/json");
			if("1".equals(request.getParamValue("COMMAND"))){ //开始查询
				
				String wrgroupType = request.getParamValue("WRGROUP_TYPE");//车型组类型
				String wrgroupCode = request.getParamValue("WRGROUP_CODE");//车型组代码
				
				//拼sql的查询条件
				if (Utility.testString(wrgroupType)) {
					sb.append(" and tg.wrgroup_type = ? ");
					params.add(wrgroupType);
				}
				if (Utility.testString(wrgroupCode)) {
					sb.append(" and upper(tg.wrgroup_code) like ? ");
					params.add("%"+wrgroupCode.toUpperCase()+"%");
				}				
				Integer curPage = request.getParamValue("curPage") != null ? Integer
						.parseInt(request.getParamValue("curPage"))
						: 1; // 处理当前页
				
				PageResult<Map<String, Object>> ps = dao.claimModelQuery(Constant.PAGE_SIZE, curPage,sb.toString(),params,oemCompanyId);
				act.setOutData("ps", ps);
			}
		} catch(BizException e){
			logger.error(logonUser,e);
			act.setException(e);
		} catch (Exception e) {
			BizException e1 = new BizException(act,e,ErrorCodeConstant.QUERY_FAILURE_CODE,"车型组维护");
			logger.error(logonUser,e1);
			act.setException(e1);
		}
	}
	/**
	 * 
	* @Title: claimModelAddInit 
	* @Description: TODO(车型组维护新增初始化) 
	* @param    
	* @return void  
	* @throws
	 */
	public void claimModelAddInit(){
		ActionContext act = ActionContext.getContext();
		AclUserBean logonUser = (AclUserBean)act.getSession().get(Constant.LOGON_USER);
		try {
			act.setForword(CLAIM_MODEL_ADD_URL);
		} catch (Exception e) {
			BizException e1 = new BizException(act,e,ErrorCodeConstant.QUERY_FAILURE_CODE,"车型组维护");
			logger.error(logonUser,e1);
			act.setException(e1);
		}		
		
	}
	/**
	 * 
	* @Title: claimModelAdd 
	* @Description: TODO(车型组维护新增) 
	* @param    
	* @return void  
	* @throws
	 */
	@SuppressWarnings("unchecked")
	public void claimModelAdd(){
		ActionContext act = ActionContext.getContext();
		AclUserBean logonUser = (AclUserBean)act.getSession().get(Constant.LOGON_USER);
		Long oemCompanyId = GetOemcompanyId.getOemCompanyId(logonUser);
		TtAsWrModelGroupPO selpo = null;
		TtAsWrModelGroupPO addpo = null;
		String errorMsg = null;
		try {
			RequestWrapper request = act.getRequest();
			String wrgroupType = request.getParamValue("WRGROUP_TYPE");  //车型组类型
			String wrgroupCode = request.getParamValue("WRGROUP_CODE");  //车型组代码
			String wrgroupName = request.getParamValue("WRGROUP_NAME");  //车型组名称
			selpo = new TtAsWrModelGroupPO();
			selpo.setWrgroupCode(wrgroupCode);
			selpo.setWrgroupType(Integer.valueOf(wrgroupType));
			//判断是否存在输入的车型组代码
			List list = dao.select(selpo);
			if(list !=null && list.size() >0){//存在
				errorMsg = wrgroupCode;
				act.setOutData("error", errorMsg);
			}else{//不存在新增一条车型组记录
				addpo = new TtAsWrModelGroupPO();
				addpo.setCreateBy(logonUser.getUserId());
				addpo.setCreateDate(new Date());
				addpo.setWrgroupCode(wrgroupCode);
				addpo.setWrgroupName(wrgroupName);
				addpo.setWrgroupType(Constant.WR_MODEL_GROUP_TYPE_01);
				addpo.setWrgroupType(new Integer(wrgroupType));
				addpo.setWrgroupId(Long.parseLong(SequenceManager.getSequence("")));
				addpo.setOemCompanyId(oemCompanyId);
				dao.insert(addpo);
				act.setOutData("success", "true");
			}
			
		} catch (Exception e) {
			BizException e1 = new BizException(act,e,ErrorCodeConstant.QUERY_FAILURE_CODE,"车型组维护");
			logger.error(logonUser,e1);
			act.setException(e1);
		}		
		
	}
	/**
	 * 
	* @Title: 
	* @Description: TODO(修改车型组初始化) 
	* @param    
	* @return void  
	* @throws
	 */
	@SuppressWarnings("unchecked")
	public void claimModelUpdateInit(){
		ActionContext act = ActionContext.getContext();
		AclUserBean logonUser = (AclUserBean)act.getSession().get(Constant.LOGON_USER);
		try {
			RequestWrapper request = act.getRequest();
			String wrgroupId = request.getParamValue("WRGROUP_ID");  //车型组id
			TtAsWrModelGroupPO selpo = new TtAsWrModelGroupPO();
			TtAsWrModelGroupPO listpo = new TtAsWrModelGroupPO();
			selpo.setWrgroupId(new Long(wrgroupId));
			List list = dao.select(selpo);
			if(list != null && list.size() > 0){
				listpo = (TtAsWrModelGroupPO)list.get(0);
			}
			act.setOutData("MODELGROUP", listpo);//存储要修改的po
			act.setForword(CLAIM_MODEL_MODIFY_URL);
		} catch (Exception e) {
			BizException e1 = new BizException(act,e,ErrorCodeConstant.QUERY_FAILURE_CODE,"车型组维护");
			logger.error(logonUser,e1);
			act.setException(e1);
		}		
		
	}
	/**
	 * 
	* @Title: claimModelUpdate 
	* @Description: TODO(车型组修改) 
	* @param    
	* @return void  
	* @throws
	 */
	public void claimModelUpdate(){
		ActionContext act = ActionContext.getContext();
		AclUserBean logonUser = (AclUserBean)act.getSession().get(Constant.LOGON_USER);
		try {
			RequestWrapper request = act.getRequest();
			String wrgroupId = request.getParamValue("WRGROUP_ID");  //车型组id
			String wrgroupType = request.getParamValue("WRGROUP_TYPE");  //车型组类型
			String wrgroupName = request.getParamValue("WRGROUP_NAME");  //车型组名称	
			String free = request.getParamValue("FREE");
			TtAsWrModelGroupPO selpo = new TtAsWrModelGroupPO();
			TtAsWrModelGroupPO updatepo = new TtAsWrModelGroupPO();
			selpo.setWrgroupId(new Long(wrgroupId));
			updatepo.setWrgroupType(new Integer(wrgroupType));
			updatepo.setWrgroupName(wrgroupName);
			updatepo.setUpdateBy(logonUser.getUserId());
			updatepo.setUpdateDate(new Date());
			updatepo.setFree(Double.valueOf(free));
			dao.claimModelUpdate(selpo,updatepo);
			act.setOutData("ACTION_RESULT","1");
		} catch (Exception e) {
			BizException e1 = new BizException(act,e,ErrorCodeConstant.QUERY_FAILURE_CODE,"车型组维护");
			logger.error(logonUser,e1);
			act.setException(e1);
		}		
		
	}
	/**
	 * 
	* @Title: claimModelDetail 
	* @Description: TODO(车型组对应的车型详细信息) 
	* @param    
	* @return void  
	* @throws
	 */
	public void claimModelDetail() {
		ActionContext act = ActionContext.getContext();
		AclUserBean logonUser = (AclUserBean)act.getSession().get(Constant.LOGON_USER);
		try {
			RequestWrapper request = act.getRequest();
			String wrgroupId = request.getParamValue("WRGROUP_ID");
			String wrgroupType = request.getParamValue("WRGROUP_TYPE");
			List addlist =  dao.getClaimModel(new Long(wrgroupId),new Integer(wrgroupType));
			
			act.setOutData("ADDLIST", addlist);//车型组里的车型详细信息
			act.setForword(CLAIM_MODEL_DETAIL_URL);
		} catch(Exception e){
			BizException e1 = new BizException(act,e,ErrorCodeConstant.DETAIL_FAILURE_CODE,"车型组维护");
			logger.error(logonUser,e1);
			act.setException(e1);
		}

	}
	/**
	 * 
	* @Title: claimModelSend 
	* @Description: TODO(车型组下发) 
	* @param    
	* @return void  
	* @throws
	 */
	public void claimModelSend(){
		
	}
	

}
